// view/my/diancan/canzgl/canzgl.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    buttonClicked: false,
    yuming: yuming,
    heights: '',
    title: "餐桌管理",
    hangs: [],
    isnew: false, //新建
    isxiu: false, //修改
    curr: 0,
    hs: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}],
    jxzh: '', //桌号
    czrslist: [],
    czrsindex: 0,
    czglrsguo: '请选择就餐人数', //人数
    xjztpx: '', //排序
    shanid: '', //删除的id
    hideshare: true,
    bgImgPath: '',
    erwei: '', //二维码
    tablename: '', //桌号
    erweis: '',
    canw: 250,
    canh: 250
  },
  // 数据
  down(e) {
    var id = e.currentTarget.dataset.id
    var tablename = e.currentTarget.dataset.tablename
    this.setData({
      tablename: tablename
    })
    var that = this
    this.myRequest(yuming + 'Besall/findVcardByUs', {
      shopId: wx.getStorageSync('shopId'),
      scene: wx.getStorageSync('shopId') + "_" + id + "_" + '3' + "_" + tablename,
      page: 'pages/shou/shou',
      // page: 'view/dian/diancan/diancan',
      // pages/dian/dian
      from: 'Consumer',
      colour: 'black'
    }, 'POST').then(res => {
      that.setData({
        erwei: res.data.QRimage
      })
      that.showshare()
    })
  },
  showshare: function () {
    wx.showLoading({
      title: '生成中',
    })
    var that = this;
    that.setData({
      hideshare: false
    }, () => {
      that.downLoad()
    })
  },
  downLoad: function (e) {
    var that = this;
    // wx.showLoading({
    //   title: '正在生成图片',
    // })
    // 二维码
    wx.downloadFile({
      url: that.data.erwei,
      success: function (res3) {
        that.setData({
          erweis: res3.tempFilePath
        })
        setTimeout(function () {
          that.drawimg();
          wx.hideLoading()
        }, 800)
      },
      fail(err) {

      }
    })

  },
  drawimg: function () {
    var that = this;
    //2. canvas绘制文字和图片
    const ctx = wx.createCanvasContext('myCanvas');
    // ctx.beginPath();
    // var tablename = that.data.tablename; //桌号
    var erweis = that.data.erweis; //二维码
    // ctx.clip()
    // 字体x位置
    // let z_x = 120
    // let z_y = 30
    ctx.fillStyle = "#FFFFFF";
    ctx.fillRect(0, 0, this.data.canw, this.data.canh);
    //桌号
    // ctx.setFontSize(18);
    // ctx.setFillStyle('#000000');
    // ctx.fillText(tablename, z_x, z_y);
    // //绘制二维码
    ctx.drawImage(erweis, 25, 25, 200, 200);
    ctx.draw();
    setTimeout(function () {
      that.savetup()
    }, 100)
  },
  savetup: function () {
    var that = this;
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 280,
      height: 300,
      destWidth: 280 * wx.getSystemInfoSync().pixelRatio,
      destHeight: 300 * wx.getSystemInfoSync().pixelRatio,
      canvasId: 'myCanvas',
      success: function (res) {
        console.log(res.tempFilePath);
        wx.showLoading({
          title: '保存中...',
          mask: true,
        });
        const tempFilePaths = res.tempFilePath
        wx.uploadFile({
          url: yuming + 'Besall/Upload',
          filePath: tempFilePaths,
          name: 'file',
          formData: {

          },
          success(res) {
            var data = JSON.parse(res.data)
            var urls = data.httpUrl + data.imgUrl
            wx.downloadFile({
              url: urls,
              success: function (res) {
                if (res.statusCode === 200) {
                  let img = res.tempFilePath;
                  wx.saveImageToPhotosAlbum({
                    filePath: img,
                    success(res) {
                      wx.showToast({
                        title: '保存成功',
                        icon: 'success'
                      });
                      setTimeout(function () {
                        that.setData({
                          hideshare: true
                        })
                      }, 1000)
                    },
                    fail(res) {
                      wx.showToast({
                        title: '保存失败',
                        icon: 'none'
                      });
                      that.setData({
                        hideshare: true
                      })
                    }
                  });
                }
              }
            });
          }
        })
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },
  // 验证
  baoyz(e) {
    var aaa = e.currentTarget.dataset.aaa
    this.setData({
      aaa: aaa
    })
    if (aaa == 0) {
      if (this.data.jxzh == '') {
        wx.showToast({
          title: '请输入桌号',
          icon: 'none'
        })
      } else if (this.data.czglrsguo == '请选择就餐人数') {
        wx.showToast({
          title: '请选择就餐人数',
          icon: 'none'
        })
      } else if (this.data.xjztpx == '') {
        wx.showToast({
          title: '请输入排序',
          icon: 'none'
        })
      } else {
        this.baoji()
      }
    } else {
      this.baoji()
    }
  },
  // 保存/修改  桌号接口
  baoji() {
    var that = this
    that.setData({
      buttonClicked: true
    })
    var url = ''
    if (that.data.aaa == 0) {
      url = 'hallfood/goodsAddTable'
    }
    if (that.data.aaa == 1) {
      url = 'hallfood/goodsSaveTable'
    }
    this.myRequest(yuming + url, {
      shopId: wx.getStorageSync('shopId'),
      id: that.data.shanid,
      tableName: that.data.jxzh,
      dinersPper: that.data.czglrsguo,
      solt: that.data.xjztpx
    }, 'POST').then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        that.setData({
          isnew: false,
          isxiu: false,
          jxzh: '',
          xjztpx: '',
          buttonClicked: false
        })
        that.canzlist()
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  // 排序
  xjztpx(e) {
    this.setData({
      xjztpx: e.detail.value
    })
  },
  // 人数
  bindczrs(e) {
    this.setData({
      czrsindex: e.detail.value,
      czglrsguo: this.data.czrslist[e.detail.value]
    })
  },
  // 新建桌号
  jxzh(e) {
    this.setData({
      jxzh: e.detail.value
    })
  },
  // 列表
  canzlist(e) {
    var that = this
    this.myRequest(yuming + 'hallfood/goodsSelectTable', {
      shopId: wx.getStorageSync('shopId')
    }, 'POST').then(res => {
      that.setData({
        hangs: res.data.data.result
      })
    })
  },
  // 切换
  qie(e) {
    var index = e.currentTarget.dataset.curr
    this.setData({
      curr: index
    })
  },
  // 新建
  xinjian(e) {
    this.setData({
      isnew: !this.data.isnew,
      jxzh: '',
      xjztpx: '',
      czglrsguo: '请选择就餐人数',
    })
  },
  // 修改
  xiu(e) {
    var id = e.currentTarget.dataset.id
    this.setData({
      isxiu: !this.data.isxiu,
      shanid: id
    })
    var that = this
    this.myRequest(yuming + 'hallfood/goodsFindTable', {
      shopId: wx.getStorageSync('shopId'),
      id: that.data.shanid
    }, 'POST').then(res => {
      that.setData({
        result: res.data.data.result,
        czglrsguo: res.data.data.result.dinersPper,
        jxzh: res.data.data.result.tableName,
        xjztpx: res.data.data.result.solt,
      })
    })
  },
  // 删除
  shanchu() {
    var that = this
    this.myRequest(yuming + 'hallfood/goodsDelTable', {
      shopId: wx.getStorageSync('shopId'),
      id: that.data.shanid
    }, 'POST').then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        that.setData({
          isxiu: false
        })
        that.canzlist()
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5
    })
    this.canzlist()
    var arr = new Array(10)
    for (var i = 1; i < arr.length + 1; i++) {
      arr[i - 1] = i;
    }
    //  console.log(arr)
    // var tt = arr.slice(17, arr.length)
    // console.log(tt)
    this.setData({
      czrslist: arr
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})